All Questions
20 questions
1vote
2answers
499views
Leetcode - find if BST is valid or not
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node'...
2votes
2answers
892views
Find all k points which are closest to origin
We have a list of points on the plane. Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the ...
1vote
1answer
62views
Depth-first traversal implementation in Java (by recursion)
I implemented a recursive depth-first traversal, with all three traversal modes in this function. Is this the most succinct way to write the code? ...
3votes
1answer
2kviews
Counting subtrees where all nodes have the same value
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value. Given the root to a binary tree, count the number of unival subtrees. For example,...
2votes
2answers
421views
Find kth smallest element in binary search tree
I was faced with below interview question today and I came up with below solution but somehow interviewer was not happy. I am not sure why.. Given a binary search tree, find the kth smallest element ...
1vote
2answers
766views
Find all root to leaf paths in binary tree
Description: Given a binary tree, return all root-to-leaf paths. Leetcode ...
3votes
1answer
1kviews
Find all k-sum paths in a binary tree
Description: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Leetcode Code: ...
2votes
1answer
522views
Find minimum depth in a binary tree
Description: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. Note: A leaf is a node ...
5votes
2answers
13kviews
Java: Given a list of edges, build a tree and return the root
In an interview I was asked to write Java code to build a tree and return the root, given a list of edges. It was a fairly open ended question where the interviewer left all the decisions up to me. I ...
4votes
1answer
368views
Construct binary tree from inorder and postorder traversal
The problem is taken from here. The tree is guaranteed not to have duplicate elements. My questions is, instead of creating new arrays leftInOrder, ...
4votes
2answers
568views
Creating a list of list after traversing binary search tree in level order
Here is my code. ...
3votes
2answers
405views
Make binary search tree from sorted array
Here is my code for converting a sorted array to a binary search tree. Please review and let me know the improvements or some better way of solving this. ...
3votes
2answers
152views
Finding if the tree is balanced or not
Can someone please review my code and let me know if there are some bugs or possible improvements? ...
8votes
1answer
774views
Are AVL trees equal?
I was inspired by this answer and decided to implement an AVL tree with the methods equals and hashCode as if I was asked to do ...
6votes
1answer
1kviews
Given a binary tree, compute the min depth of a leaf node
Here is the source of the question. They don't require a tree to be a BST, but my tree is. The algorithm would be the same for a simple binary tree. And there are two implementations: A recursive one ...